<template>
    <el-dialog
            title="关闭预警"
            :close-on-click-modal="false"
            :visible.sync="dialogParams.visible"
            width="35%">
        <el-form ref="formData" :rules="rules" :model="formData" label-width="120px">
            <el-form-item label="预警关闭原因" prop="warningCloseDesc">
                <el-input
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 8}"
                        placeholder="请输入预警关闭原因"
                        v-model.trim="formData.warningCloseDesc">
                </el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close('formData')">关 闭</el-button>
            <el-button type="primary" @click="closeWarning('formData')">保 存</el-button>
      </span>
    </el-dialog>
</template>

<script>
    import {updateWarningState} from "@/api/improve";

    export default {
        name: 'closeWarning',
        props: {
            dialogParams: {
                type: Object
            }
        },
        data () {
            return {
                formData: {
                    warningCloseDesc: '',
                },
                rules: {
                    warningCloseDesc: [
                        {required: true, message: '请输入预警关闭原因', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            closeWarning(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        // warningState: 0-未预警 1-预警中 2-关闭预警
                        let params = {
                            id: this.dialogParams.id,
                            warningColseDesc: this.formData.warningCloseDesc,
                            warningState: 2
                        };
                        updateWarningState(params).then(res => {
                            this.$message.success('操作成功');
                            this.dialogParams.visible = false;
                            this.$emit('getList')
                        }).catch(() => {})
                    } else {
                        return false;
                    }
                });
            },
            // 关闭
            close (formName) {
                this.$refs[formName].resetFields();
                this.dialogParams.visible = false;
            }
        }
    }
</script>

<style scoped>

</style>